<?php

class template
{
	protected $title = '';	// 标题

	protected $meta_keyword = '';	// meta
	protected $meta_description = '';

	protected $data = array();		// 数据

	protected $pagination = null;	// 分页类

	protected $editor = false;		// 编辑器是否已加载

	// 网站界面布局 为 false 时表示没有该区域
	protected $north = true;		// 顶部
	protected $west = true;			// 左栏
	protected $center = true;		// 中部
	protected $east = true;			// 右栏
	protected $south = true;		// 底部

	public function __construct(){}

	public function display()
	{
		echo '这是一个 Mr Bone 模板';
	}

	protected function head(){return;}	// <head></head>头可加 js/css
	
	protected function body(){return;}	// <body></body>中间的部分 分为五个区:	north:项部, west:主体左部, center:主体中部, east:主体右部, south:底部
	
	protected function north(){return;}	// 项部
	protected function middle(){return;}// 主体 分为三个区:	west:主体左部, center:主体中部, east:主体右部
	protected function south(){return;}	// 底部

	// 主体左中右三栏
	protected function west(){return;}	// 主体左部
	protected function center(){return;}// 主体中部
	protected function east(){return;}	// 主体右部



	protected function message(){return;}	//提示信息
	protected function pagination(){return;}	//输出分页代码
	protected function editor($editor_name, $width, $height){return;}// 编辑器


	
	// 赋对象值
	public function set_data($data)
	{
		if($data)
			$this->data = array_merge($this->data, $data);
    }

	// 赋值
	public function set($name, $value)
	{
        $this->data[$name] = $value;
    }

	// 取值
	public function get($name)
	{
		$value = null;
		if(isset($this->data[$name])) $value = $this->data[$name];
		return $value;
	}

	public function get_title(){return $this->title;}
	public function set_title($title){$this->title = $title;}

	public function get_meta_keyword(){return $this->meta_keyword;}
	public function set_meta_keyword($meta_keyword){$this->meta_keyword = $meta_keyword;}

	public function get_meta_description(){return $this->meta_description;}
	public function set_meta_description($meta_description){$this->meta_description = $meta_description;}

	// 获得分页
	public function get_pagination()
	{ 
		if(!$this->pagination) $this->pagination = new pagination();
		return $this->pagination; 
	}

}


class pagination
{
	private $total = 0;				// 总记录数
	private $pages = 0;				// 总页数
	private $limit = 20;			// 每页记录数
	private $page = 1;				// 页码
	private $url = '';				// 链接

	// 构造函数
	public function __construct(){}

	// 析构函数
	public function __destruct(){}

	// 设置当前第几页
	public function set_page( $page )
	{
		$page = intval( $page );
		if( $page<0 ) $page = 1;
		$this->page = $page;

		$this->update();
	}

	// 设置总记录数
	public function set_total( $total )
	{
		$total = intval( $total );
		if( $total<0 ) $total = 0;
		$this->total = $total;

		$this->update();
	}

	// 设置链接
	public function set_url( $url )
	{
		if( strpos($url, '?') === false)
			$url .= '?';
		else
			$url .= '&';
		$this->url = $url;
	}

	// 设置第页显示多少条记录
	public function set_limit( $limit )
	{
		$limit = intval( $limit );
		if( $limit<0 ) $limit = 10;
		$this->limit = $limit;

		$this->update();
	}

	// 更新
	private function update()
	{
		$this->pages = ceil($this->total / $this->limit);
		if( $this->page > $this->pages ) $this->page = $this->pages;
		if( $this->page < 1 ) $this->page = 1;
	}

	public function get_total(){ return $this->total; }
	public function get_pages(){ return $this->pages; }
	public function get_limit(){ return $this->limit; }
	public function get_page(){ return $this->page; }
	public function get_url(){ return $this->url; }
	public function get_offset(){ return ($this->page-1)*$this->limit; }

}
